﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using System.Configuration;

public partial class CalendarQuery_DiaryContentShow : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            if(Request.QueryString.Get("ud_id") != null)
            {
                int ud_id = int.Parse(Request.QueryString.Get("ud_id"));
                FindDiaryInfo(ud_id);
                DataList_DataTable(ud_id);
            }
        }
    }

    protected void FindDiaryInfo(int ud_id)
    {
        UserDiarytbService udService = new UserDiarytbService();
        UserDiary ud = udService.findByDiaryId(ud_id);
        if(ud != null)
        {
            LblDiaryTitle.Text = ud.DiaryTitle;
            LblTime.Text = ud.DiaryUser.UserName + " 发表于:" + ud.RegisterTime.ToString();
            LblDiaryContent.Text = ud.DiaryContent;
        }
    }

    protected void DataList_DataTable(int ud_id)
    {
        DiaryFileUploadMappingtbService dfService = new DiaryFileUploadMappingtbService();
        List<DiaryFileUploadMappingtb> list = dfService.findByDiaryId(ud_id);
        string fileRoot = ConfigurationManager.AppSettings["DiaryFilePath"];
        if(list != null && list.Count > 0)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName", typeof(string));
            dt.Columns.Add("FilePath", typeof(string));
            foreach(DiaryFileUploadMappingtb df in list)
            {
                DataRow dr = dt.NewRow();
                dr["FileName"] = df.FileName;
                dr["FilePath"] = df.FilePath.Replace(fileRoot, "");
                dt.Rows.Add(dr);
            }
            DataList1.DataSource = dt.DefaultView;
        }
        else
        {
            DataList1.DataSource = null;
        }
        DataList1.DataBind();
    }
    protected void LbtDownLoad_Click(object sender, EventArgs e)
    {
        string fileName = ((LinkButton) sender).Attributes["f_n"];
        string filePath = ((LinkButton) sender).Attributes["f_p"];
        string fileRoot = FindConfigurationPath.instance().DiaryFilePath + filePath;
        FileInfo fi = new FileInfo(fileRoot);
        if(fi.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
    }
}
